package controllers

import (
	"encoding/json"
	"fmt"
	"goetl/models"
	"goetl/services"

	"github.com/astaxie/beego"
)

type ConfigurationController struct {
	beego.Controller
}

func (c *ConfigurationController) URLMapping() {
	c.Mapping("FindProjects", c.FindProjects)
	c.Mapping("FindProjectVersionsByProject", c.FindProjectVersionsByProject)

}

// @router /services/ConfigurationService/FindProjects [get]
func (c *ConfigurationController) FindProjects() {

	err, projects := services.ConfigurationService.FindProjects()

	if err == nil {
		c.Data["json"] = &projects
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindProjectVersionsByProject [get]
func (c *ConfigurationController) FindProjectVersionsByProject() {

	err, projectVersions := services.ConfigurationService.FindProjectVersionsByProject(c.GetString("projectId"))

	if err == nil {
		c.Data["json"] = &projectVersions
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindFlowsInProject [post]
func (c *ConfigurationController) FindFlowsInProject() {

	istest, _ := c.GetInt32("test")
	err, flows := services.ConfigurationService.FindFlowsInProject(c.GetString("projectVersionId"), istest)

	if err == nil {
		c.Data["json"] = &flows
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindModelsInProject [post]
func (c *ConfigurationController) FindModelsInProject() {

	err, models := services.ConfigurationService.FindModelsInProject(c.GetString("projectVersionId"))

	if err == nil {
		c.Data["json"] = &models
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindResourcesInProject [post]
func (c *ConfigurationController) FindResourcesInProject() {

	err, resources := services.ConfigurationService.FindResourcesInProject(c.GetString("projectVersionId"))

	if err == nil {
		c.Data["json"] = &resources
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindProjectDependencies [post]
func (c *ConfigurationController) FindProjectDependencies() {

	err, p := services.ConfigurationService.FindProjectDependencies(c.GetString("projectVersionId"))

	if err == nil {
		c.Data["json"] = &p
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindResource [post]
func (c *ConfigurationController) FindResource() {

	err, p := services.ConfigurationService.FindResource(c.GetString("id"))

	if err == nil {
		c.Data["json"] = &p
		c.ServeJSON()
	}
}

// @router /services/ConfigurationService/FindResourceDefinition [post]
func (c *ConfigurationController) FindResourceDefinition() {

	p := services.ConfigurationService.FindResourceDefinition(c.GetString("id"))

	c.Data["json"] = &p
	c.ServeJSON()

}

// @router /services/ConfigurationService/FindResourceSettting [post]
func (c *ConfigurationController) FindResourceSettting() {

	err, p := services.ConfigurationService.FindResourceSettting(c.GetString("id"))

	if err == nil {
		c.Data["json"] = &p
		c.ServeJSON()
	}

}

// @router /services/ConfigurationService/SaveResourceSettings [post]
func (c *ConfigurationController) SaveResourceSettings() {

	user := c.GetSession("user").(models.User)

	dataStr := c.GetString("data")
	var data []models.SaveResourceSetting

	err := json.Unmarshal([]byte(dataStr), &data)
	c.Data["json"] = map[string]interface{}{"result": "true"}
	//fmt.Println(dataStr)
	if err == nil {
		//fmt.Println(data)
		err = services.ConfigurationService.SaveResourceSettings(data, user)
		if err != nil {
			c.Data["json"] = map[string]interface{}{"result": "false"}
		}
	} else {
		fmt.Println(err)
	}

	c.ServeJSON()
}

// @router /services/ConfigurationService/FindModel [post]
func (c *ConfigurationController) FindModel() {

	err, p := services.ConfigurationService.FindModel(c.GetString("id"))

	if err == nil {
		c.Data["json"] = &p
		c.ServeJSON()
	}

}

// @router /services/ConfigurationService/FindComponent [post]
func (c *ConfigurationController) FindComponent() {
	readRelations, _ := c.GetBool("readRelations")
	err, p := services.ConfigurationService.FindComponent(c.GetString("componentId"), readRelations)

	if err == nil {
		c.Data["json"] = &p
		c.ServeJSON()
	}

}
